//
//  VisitorView.swift
//  weibo
//
//  Created by David on 15/11/14.
//  Copyright © 2015年 DV. All rights reserved.
//

import UIKit
protocol clickDelegate:NSObjectProtocol
{
    func loginWillClick()
}
class VisitorView: UIView {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    //提供一个方法
    func loadInfo(imageName:String?,text:String){
        self.messageLabel.text = text
        if let imageName = imageName{
            self.houseView.image = UIImage(named: imageName)
            self.iconView.hidden = true
        }else{
            startAnim()
        }
        
    }
    private func startAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false

        iconView.layer.addAnimation(anim, forKey: nil)
    }

    weak var delegate : clickDelegate?
//    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView : UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

        
        return imageView
    }()
    private lazy var houseView : UIImageView = UIImageView (image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var messageLabel : UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些事 关注一些事关注一些事关注一些事关注一些事关注一些事"
        label.textColor = UIColor.grayColor()
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        label.sizeToFit()
        
        return label
    }()
    private lazy var registerBTN : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        return btn
    }()

    private lazy var loginBTN : UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登入", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginClick", forControlEvents: .TouchUpInside)
        return btn
    }()
     func loginClick(){
            delegate?.loginWillClick()
        
    }
   override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        backgroundColor = UIColor.whiteColor()
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    private func setupUI() {
        addSubview(iconView)
        startAnim()
        addSubview(houseView)
        addSubview(loginBTN)
        addSubview(messageLabel)
        let dict : [String:AnyObject] = ["iconView":iconView,"loginView":loginBTN,"messageLabel":messageLabel]
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        houseView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint((NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)))
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute:NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 200))
        
        loginBTN.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[messageLabel]-(10)-[loginView]", options: [], metrics: nil, views:dict ))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[iconView]-[loginView]", options: [], metrics: nil, views: dict))
        addConstraint(NSLayoutConstraint(item: loginBTN, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBTN, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50))
        
        
    }

}
